﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BOSS.Framework;

namespace BOSS.Domain.Security
{
    /// <summary>
    /// 角色
    /// </summary>
    [Serializable]
    public class Role : Entity<int>, IPrivilegeContainer
    {
        /// <summary>
        /// 获取 / 设置 标识符
        /// </summary>
        public virtual Guid? Guid { get; set; }

        /// <summary>
        /// 获取 / 设置 角色名称
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// 获取 / 设置 显示名称
        /// </summary>
        public virtual string DisplayName { get; set; }

        /// <summary>
        /// 获取 / 设置 描述信息
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// 获取 / 设置 是否和域断开
        /// </summary>
        public virtual bool Detached { get; set; }

        /// <summary>
        /// 获取 / 设置 是否可以指定权限
        /// </summary>
        public virtual bool PrivilegeAssignable { get; set; }

        /// <summary>
        /// 获取 / 设置 拥有此角色的帐号集合
        /// </summary>
        public virtual IList<Account> Accounts { get; set; }

        /// <summary>
        /// 获取 是否来自域
        /// </summary>
        public virtual bool FromDomain
        {
            get
            {
                return this.Guid != null;
            }
        }

        /// <summary>
        /// 获取 该角色是否可以删除
        /// </summary>
        public virtual bool Deletable
        {
            get
            {
                return (!this.FromDomain || this.Detached) && this.PrivilegeAssignable;
            }
        }

        /// <summary>
        /// 添加一个帐号（会自动在 Account 中建立相应关联）
        /// </summary>
        /// <param name="account"></param>
        public virtual void AddAccount(Account account)
        {
            InternalAddAccount(account);
            account.InternalAddRole(this);
        }

        /// <summary>
        /// 移除一个帐号（会自动在 Account 中删除相应关联）
        /// </summary>
        /// <param name="account"></param>
        public virtual void RemoveAccount(Account account)
        {
            InternalRemoveAccount(account);
            account.InternalRemoveRole(this);
        }

        /// <summary>
        /// 添加一个帐号
        /// </summary>
        /// <param name="account"></param>
        protected internal virtual void InternalAddAccount(Account account)
        {
            Check.Require(account != null);

            if (this.Accounts == null)
            {
                this.Accounts = new List<Account>();
            }
            this.Accounts.Add(account);
        }

        /// <summary>
        /// 移除一个帐号
        /// </summary>
        /// <param name="account"></param>
        protected internal virtual void InternalRemoveAccount(Account account)
        {
            Check.Require(account != null);

            if (this.Accounts == null)
            {
                this.Accounts = new List<Account>();
            }
            this.Accounts.Remove(account);
        }

        /// <summary>
        /// 获取文字串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0} ({1})", this.Name, this.DisplayName);
        }
    }
}
